<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 输入框区域 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表数据区域 -->
      <el-table :data="orderList" style="width: 100%" stripe border>
        <el-table-column type="index" label="#" :resizable="false">
        </el-table-column>
        <el-table-column
          prop="order_number"
          label="订单编号"
          :resizable="false"
        >
        </el-table-column>
        <el-table-column
          prop="order_price"
          label="订单价格(元)"
          :resizable="false"
          width="150px"
        >
        </el-table-column>
        <el-table-column
          prop="pay_status"
          label="是否付款"
          :resizable="false"
          width="110px"
        >
          <template v-slot="shuju">
            <el-tag type="success" v-if="shuju.row.pay_status === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="is_send"
          label="是否发货"
          :resizable="false"
          width="110px"
        >
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="下单时间"
          :resizable="false"
          width="180px"
        >
          <template v-slot="shuju">
            {{ shuju.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" :resizable="false" width="200px">
          <template v-slot="">
            <!-- 修改按键 -->
            <el-tooltip content="修改地址" placement="top" :enterable="false">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="small"
                @click="showEditAddress"
              ></el-button>
            </el-tooltip>
            <!-- 物流进度按键 -->
            <el-tooltip content="物流进度" placement="top" :enterable="false">
              <el-button
                type="success"
                icon="el-icon-location"
                size="small"
                @click="showProgress"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改收货地址"
      :visible.sync="addressVisible"
      width="50%"
      @close="addressDialogClosed"
    >
      <!-- 添加表单 -->
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
            v-model="addressForm.address1"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 物流信息进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <!-- 时间线组件  -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.timestamp"
        >
          {{ activity.content }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata'
export default {
  name: 'Order',
  data() {
    return {
      // 查询订单列表参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      // 订单列表数据
      orderList: [],
      // 总数据条数
      total: 0,
      // 修改地址对话框是否显示
      addressVisible: false,
      // 修改收货地址的表单
      addressForm: {
        address1: [],
        address2: '',
      },
      // 修改表单的验证规则
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
        ],
      },
      // 中国城市数据
      cityData,
      // 物流进度对话框是否显示
      progressVisible: false,
      // 保存物流信息
      progressInfo: [
        {
          content: '活动按期开始',
          timestamp: '2018-04-15',
        },
        {
          content: '通过审核',
          timestamp: '2018-04-13',
        },
        {
          content: '创建成功',
          timestamp: '2018-04-11',
        },
      ],
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 根据 订单列表参数 获取对应的 订单列表
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message({
          duration: 2000,
          message: '获取订单列表失败！',
          type: 'error',
        })
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 监听 pageSize 的改变事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 监听页码值改变事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 展示修改地址的对话框
    showEditAddress() {
      this.addressVisible = true
    },
    // 监听修改地址对话框的关闭事件
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    // 展示物流进度对话框
    async showProgress() {
      // 发送请求获取物流数据 测试数据

      // 显示对话框
      this.progressVisible = true
    },
  },
}
</script>

<style scoped>
.el-cascader {
  width: 100%;
}
</style>
